package com.pitaya.framework.common;

import java.io.InputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.baidu.inf.iis.bcs.BaiduBCS;
import com.baidu.inf.iis.bcs.auth.BCSCredentials;
import com.baidu.inf.iis.bcs.http.HttpMethodName;
import com.baidu.inf.iis.bcs.model.ObjectMetadata;
import com.baidu.inf.iis.bcs.request.GenerateUrlRequest;
import com.baidu.inf.iis.bcs.request.PutObjectRequest;

public class BCSFileUtils {
	public static final Log log = LogFactory.getLog(BCSFileUtils.class);
	public static final String ACCESS_KEY = "KMfpaLCOZIiReUDVjrdGAvCD";
	public static final String SECRET_KEY = "kCGGrVNDETzzPUXzfTc6evdtdzFAYgGm";
	public static String HOST = "bcs.duapp.com";
	public static final String BUCKET = "winwine";

	public static String uploadFile(InputStream inputStream, String fileName, long contentLength) {
		BCSCredentials credentials = new BCSCredentials(ACCESS_KEY, SECRET_KEY);
		BaiduBCS baiduBCS = new BaiduBCS(credentials, HOST);
		baiduBCS.setDefaultEncoding("UTF-8");
		ObjectMetadata objectMetadata = new ObjectMetadata();
		objectMetadata.setContentType("image/*");
		objectMetadata.setContentLength(contentLength);
		PutObjectRequest request = new PutObjectRequest(BUCKET, fileName, inputStream, objectMetadata);
		baiduBCS.putObject(request).getResult();
		GenerateUrlRequest generateUrlRequest = new GenerateUrlRequest( HttpMethodName.GET, BUCKET, fileName);
		return baiduBCS.generateUrl(generateUrlRequest);
	}
}
